﻿<%@ Application Language="C#" %>

<script runat="server">
    ////session between www and http asp.net
    //public void Application_BeginRequest(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        string requestedDomain = HttpContext.Current.Request.Url.ToString().ToLower();
    //        if (requestedDomain.Contains("http://caomeo.com"))
    //        {
    //            requestedDomain = requestedDomain.Replace("http://caomeo.com", "http://www.caomeo.com");
    //            Response.Clear();
    //            Response.Status = "301 Moved Permanently";
    //            Response.AddHeader("Location", requestedDomain);
    //            Response.End();
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        //Response.Write("Error in Global.asax :" + ex.Message);
    //    }

    //}
    
    
    //public static int hitCount = 0;
    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        Application["FCKeditor:UserFilesPath"] = "../../../../../img/";
        Application["OnlineCounter"] = 0;
        
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        
        // Code that runs when a new session is started
        
        try
        {
            //-------------------B1. Lấy số lượt truy cập từ db ra và tăng lên
            CommonDAO cd = new CommonDAO();
            decimal hitCount = cd.chayCauSQLScalarDecimal("select hitCount from HitCount");
			hitCount++;
            //-------------------B2. Update db
            //if(hitCount%5==0)
            //{
            cd.chayCauUpdate("update HitCount set hitcount=hitCount+1");
            //}
            //-------------------B3. Update session
			Application.Lock();
            Application["OnlineCounter"] = TTQT.TTQT_Code.TTQTCommon.ToInt32(Application["OnlineCounter"], 0) + 1;
            Application["HitCount"] = hitCount;			
            Application.UnLock();

			
            
        }catch(Exception ex){}
        
        
    }

    void Session_End(object sender, EventArgs e)
    {
        Application.Lock();
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
        try
        {
            int numOnline = TTQT.TTQT_Code.TTQTCommon.ToInt32(Application["OnlineCounter"], 0);
            if (numOnline > 0)
            {
                Application["OnlineCounter"] = numOnline - 1;
            }
        }catch(Exception exx){}
        
        Application.UnLock();
    }
       

</script>
